﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_USERS 100
#define MAX_NAME_LENGTH 50
#define PASSWORD_LENGTH 10

typedef struct {
    char name[MAX_NAME_LENGTH];
    char password[PASSWORD_LENGTH];
    float balance;
} Account;

Account accounts[MAX_USERS];
int accountCount = 0;

// 函数声明
void createUser();
void loginSystem();
void displayAccountInfo();
void depositMoney();
void withdrawMoney();
void mainMenu();
// 创建用户账户
void createUser() {
    if (accountCount >= MAX_USERS) {
        printf("Cannot create more users. Max limit reached.\n");
        return;
    }

    Account newAccount;
    printf("Enter name: ");
    scanf_s("%s", newAccount.name);
    printf("Enter password: ");
    scanf_s("%s", newAccount.password);
    newAccount.balance = 0; // 初始余额为0

    accounts[accountCount++] = newAccount;
    printf("User created successfully.\n");
}

// 用户登录系统
void loginSystem() {
    char username[MAX_NAME_LENGTH], password[PASSWORD_LENGTH];
    printf("Enter username: ");
    scanf_s("%s", username);
    printf("Enter password: ");
    scanf_s("%s", password);

    for (int i = 0; i < accountCount; i++) {
        if (strcmp(accounts[i].name, username) == 0 && strcmp(accounts[i].password, password) == 0) {
            printf("Login successful.\n");
            mainMenu();
            return;
        }
    }

    printf("Invalid username or password.\n");
}

// 显示账户信息
void displayAccountInfo() {
    // 假设只有一个用户登录，因此只显示该用户的账户信息
    printf("Name: %s\n", accounts[0].name);
    printf("Balance: %.2f\n", accounts[0].balance);
}

// 存款功能
void depositMoney() {
    float amount;
    printf("Enter amount to deposit: ");
    scanf_s("%f", &amount);
    accounts[0].balance += amount;
    printf("Deposit successful.\n");
}

// 取款功能
void withdrawMoney() {
    float amount;
    printf("Enter amount to withdraw: ");
    scanf_s("%f", &amount);
    if (amount > accounts[0].balance) {
        printf("Insufficient balance.\n");
    }
    else {
        accounts[0].balance -= amount;
        printf("Withdrawal successful.\n");
    }
}

// 主菜单
void mainMenu() {
    int choice;
    do {
        printf("\nMain Menu:\n");
        printf("1. Display Account Info\n");
        printf("2. Deposit Money\n");
        printf("3. Withdraw Money\n");
        printf("4. Logout\n");
        printf("Enter choice: ");
        scanf_s("%d", &choice);

        switch (choice) {
        case 1:
            displayAccountInfo();
            break;
        case 2:
            depositMoney();
            break;
        case 3:
            withdrawMoney();
            break;
        case 4:
            printf("User logged out.\n");
            return;
        default:
            printf("Invalid choice. Please try again.\n");
        }
    } while (choice != 4);
}

// 主函数
int main() {
    int initialSetup = 1; // 假设系统是首次运行，需要创建初始用户
    if (initialSetup) {
        createUser(); // 创建一个初始用户账户
        initialSetup = 0; // 标记系统已经设置了初始用户
    }

    loginSystem(); // 用户登录系统

    return 0;
}